﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using MoneyManagerNET.Model;


namespace MoneyManagerNET.DAL.Repository.EF4
{
    public class EntityFrameworkBusBase<TContext>:IEntityBus where  TContext:ObjectContext,new()
    {
        ObjectContext EntityFrameworkContext;


        Dictionary<Type, Type> interface_mapping = new Dictionary<Type, Type>();

        public EntityFrameworkBusBase()
        {
            EntityFrameworkContext = new TContext();

            //interface_mapping.Add(typeof(Model.IAccount),typeof(Account));
            //interface_mapping.Add(typeof(Model.IBankAccount), typeof(BankAccount));
            //interface_mapping.Add(typeof(Model.IStockAccount), typeof(StockAccount));
            //interface_mapping.Add(typeof(Model.ICurrency), typeof(Currency));
            //interface_mapping.Add(typeof(Model.ICategory), typeof(Category));
            //interface_mapping.Add(typeof(Model.ISubCategory), typeof(SubCategory));
            //interface_mapping.Add(typeof(Model.IContragent), typeof(Contragent));
            //interface_mapping.Add(typeof(Model.ITransaction), typeof(Transaction));
            //interface_mapping.Add(typeof(Model.IStockInvestment), typeof(StockInvestment));

        }

        private IEnumerable<TEntity> SelectListByType<TEntity>() where TEntity : class
        {
            System.Collections.IEnumerable e = null;
            if (typeof(TEntity) == typeof(Model.ITransaction)) e= EntityFrameworkContext.CreateObjectSet<Transaction>() ;
            if (typeof(TEntity) == typeof(Model.ISubCategory)) e= EntityFrameworkContext.CreateObjectSet<SubCategory>();
            if (typeof(TEntity) == typeof(Model.ICategory)) e= EntityFrameworkContext.CreateObjectSet<Category>();
            if (typeof(TEntity) == typeof(Model.IStockInvestment)) e= EntityFrameworkContext.CreateObjectSet<StockInvestment>();
            if (typeof(TEntity) == typeof(Model.IStockAccount)) e= EntityFrameworkContext.CreateObjectSet<StockAccount>();
            if (typeof(TEntity) == typeof(Model.ICurrency)) e= EntityFrameworkContext.CreateObjectSet<Currency>();
            if (typeof(TEntity) == typeof(Model.IContragent)) e= EntityFrameworkContext.CreateObjectSet<Contragent>();
            if (typeof(TEntity) == typeof(Model.IBankAccount)) e = EntityFrameworkContext.CreateObjectSet<BankAccount>();
            if (typeof(TEntity) == typeof(Model.IAccount)) e= EntityFrameworkContext.CreateObjectSet<Account>();

            return e  as IEnumerable<TEntity>;
        }
        public IEnumerable<TEntity> Select<TEntity>(Func<TEntity, bool> where) where TEntity : class
        {

            var entitySet = SelectListByType<TEntity>();
            var q=entitySet.AsQueryable<TEntity>();
            var w=q.Where(where);
            return w.AsQueryable<TEntity>();
        }

        public void SaveAll()
        {
            EntityFrameworkContext.SaveChanges();
        }

        public void Reset()
        {
            throw new NotImplementedException();
        }


        public TEntity New<TEntity>() where TEntity : class
        {
            return EntityFrameworkContext.CreateObject<TEntity>();
        }


        public void Delete<TEntity>(TEntity entity) where TEntity : class
        {
            EntityFrameworkContext.DeleteObject(entity);
        }
        
    }
}
